package com.example.tms.controller;

import com.example.tms.bean.CstmrInfo;
import com.example.tms.bean.ReturnInfo;
import com.example.tms.mapper.CstmrMapper;
import com.example.tms.tools.GlobalParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CstmrController {

    private final CstmrMapper cstmrMapper;

    @Autowired
    public CstmrController(CstmrMapper cstmrMapper){
        this.cstmrMapper = cstmrMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询客户信息列表
    @RequestMapping(value = "cstmr",method = RequestMethod.GET)
    public ReturnInfo getCstmrinfoAll(@RequestParam(required = false, defaultValue = "") String khjc,
                                      @RequestParam(required = false, defaultValue = "") String khzjm){
        List<CstmrInfo> result = cstmrMapper.findAll((Root<CstmrInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if(!khjc.equals("")){
                list.add(criteriaBuilder.like(root.get("khjc").as(String.class),"%" + khjc + "%"));
            }
            if(!khzjm.equals("")){
                list.add(criteriaBuilder.like(root.get("khzjm").as(String.class),"%" + khzjm + "%"));
            }
            list.add(criteriaBuilder.equal(root.get("sfcz").as(Boolean.class),true));
            Predicate[] p = new Predicate[list.size()];
            criteriaQuery.where(criteriaBuilder.and(list.toArray(p)));
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("lasttime")));
            return criteriaQuery.getRestriction();
        });
        return ReturnInfo.info2(result);
    }

    //查询客户信息
    @RequestMapping(value = "cstmr/{khid}",method = RequestMethod.GET)
    public CstmrInfo getCstmrinfo(@PathVariable String khid){
        return cstmrMapper.findByKhid(khid);
    }

    //新建客户信息
    @RequestMapping(value = "cstmr",method = RequestMethod.POST)
        public List<Map<String,Object>> saveCstmrinfo(@RequestBody CstmrInfo cstmrInfo){
        List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
        Map<String,Object>  map = new HashMap<String,Object>();
        if(cstmrInfo.getKhjc().equals("")||!checkKhjc(cstmrInfo.getKhjc())){
            map.put("khjc", "0");
        }
        if(!cstmrInfo.getKhzjm().equals("")&&!checkKhzjm(cstmrInfo.getKhzjm())){// 非必填项
            map.put("khzjm", "0");
        }
        if(!cstmrInfo.getKhqc().equals("")&&!checkKhqc(cstmrInfo.getKhqc())){// 非必填项
            map.put("khqc", "0");
        }
        if(!cstmrInfo.getLeibie().equals("")&&!checkLeibei(cstmrInfo.getLeibie())){// 非必填项
            map.put("leibie", "0");
        }
        if(!cstmrInfo.getYhzh().equals("")&&!checkYhzh(cstmrInfo.getYhzh())){ // 非必填项
            map.put("yhzh", "0");
        }
        if(!cstmrInfo.getKhsjhm().equals("")&&!checkKhsjhm(cstmrInfo.getKhsjhm())){ // 非必填项
            map.put("khsjhm", "0");
        }
        if(!cstmrInfo.getShuihao().equals("")&&!checkShuihao(cstmrInfo.getShuihao())){ // 非必填项
            map.put("shuihao", "0");
        }
        if(!cstmrInfo.getKhlxdh().equals("")&&!checkKhlxdh(cstmrInfo.getKhlxdh())){ // 非必填项
            map.put("khlxdh", "0");
        }
        if(!cstmrInfo.getKhlxr().equals("")&&!checkKhlxr(cstmrInfo.getKhlxr())){ // 非必填项
            map.put("khlxr", "0");
        }
        if(!cstmrInfo.getKhczhm().equals("")&&!checkKhczhm(cstmrInfo.getKhczhm())){ // 非必填项
            map.put("khczhm", "0");
        }
        if(!cstmrInfo.getKhfl().equals("")&&!checkKhfl(cstmrInfo.getKhfl())){// 非必填项
            map.put("khfl", "0");
        }
        if(!cstmrInfo.getLxdz().equals("")&&!checkLxdz(cstmrInfo.getLxdz())){// 非必填项
            map.put("lxdz", "0");
        }
        list.add(map);
        if(map.isEmpty()){
            cstmrInfo.setLasttime(GlobalParam.getTime());
            cstmrInfo.setSfcz(true);
            cstmrMapper.save(cstmrInfo);
            logger.info("新增客户"+cstmrInfo.toString());
        }
        return list;
    }

    //更新客户信息
    @RequestMapping(value = "cstmr",method = RequestMethod.PUT)
    public List<Map<String,Object>> updateCstmrinfo(@RequestBody CstmrInfo cstmrInfo){
        List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
        Map<String,Object>  map = new HashMap<String,Object>();
        if(cstmrInfo.getKhid()==null||cstmrMapper.findByKhid(cstmrInfo.getKhid())==null){
            map.put("khid", "0");
        }
        if(cstmrInfo.getKhjc().equals("")||!checkKhjc(cstmrInfo.getKhjc())){
            map.put("khjc", "0");
        }
        if(!cstmrInfo.getKhzjm().equals("")&&!checkKhzjm(cstmrInfo.getKhzjm())){// 非必填项
            map.put("khzjm", "0");
        }
        if(!cstmrInfo.getKhqc().equals("")&&!checkKhqc(cstmrInfo.getKhqc())){// 非必填项
            map.put("khqc", "0");
        }
        if(!cstmrInfo.getLeibie().equals("")&&!checkLeibei(cstmrInfo.getLeibie())){// 非必填项
            map.put("leibie", "0");
        }
        if(!cstmrInfo.getYhzh().equals("")&&!checkYhzh(cstmrInfo.getYhzh())){ // 非必填项
            map.put("yhzh", "0");
        }
        if(!cstmrInfo.getKhsjhm().equals("")&&!checkKhsjhm(cstmrInfo.getKhsjhm())){ // 非必填项
            map.put("khsjhm", "0");
        }
        if(!cstmrInfo.getShuihao().equals("")&&!checkShuihao(cstmrInfo.getShuihao())){ // 非必填项
            map.put("shuihao", "0");
        }
        if(!cstmrInfo.getKhlxdh().equals("")&&!checkKhlxdh(cstmrInfo.getKhlxdh())){ // 非必填项
            map.put("khlxdh", "0");
        }
        if(!cstmrInfo.getKhlxr().equals("")&&!checkKhlxr(cstmrInfo.getKhlxr())){ // 非必填项
            map.put("khlxr", "0");
        }
        if(!cstmrInfo.getKhczhm().equals("")&&!checkKhczhm(cstmrInfo.getKhczhm())){ // 非必填项
            map.put("khczhm", "0");
        }
        if(!cstmrInfo.getKhfl().equals("")&&!checkKhfl(cstmrInfo.getKhfl())){// 非必填项
            map.put("khfl", "0");
        }
        if(!cstmrInfo.getLxdz().equals("")&&!checkLxdz(cstmrInfo.getLxdz())){// 非必填项
            map.put("lxdz", "0");
        }
        list.add(map);
        if(map.isEmpty()) {
            cstmrInfo.setLasttime(GlobalParam.getTime());
            cstmrInfo.setSfcz(true);
            cstmrMapper.saveAndFlush(cstmrInfo);
            logger.info("更新客户"+cstmrInfo.toString());
        }
        return list;
    }

    //删除客户信息（假删除 客户id）
    @RequestMapping(value = "cstmr/{khid}",method = RequestMethod.DELETE)
    public ReturnInfo deleteCstmrinfo(@PathVariable String khid){
        String b;
        if(cstmrMapper.findByKhid(khid)==null){
            b = "删除失败！";
        }else{
            cstmrMapper.DeleteByKhid(khid);
            b = "删除成功！";
        }
        logger.info("删除客户"+khid);
        return ReturnInfo.info1(b);
    }

    //检查客户类别格式
    private Boolean checkLeibei(String leibie){
        Boolean b = false;
        if(leibie.equals("大客户")||leibie.equals("零散客户")){
            b = true;
        }
        return b;
    }

    //检查客户分类格式
    private Boolean checkKhfl(String khfl){
        Boolean b = false;
        if(khfl.equals("托运方")||khfl.equals("收货方")){
            b = true;
        }
        return b;
    }
    //检查银行账号格式
    private Boolean checkYhzh(String yhzh){
        Boolean b = false;
        if(yhzh.matches("^[0-9 ]{1,23}$")){
            b = true;
        }
        return b;
    }
    //检查手机号码格式
    private Boolean checkKhsjhm(String khsjhm){
        Boolean b = false;
        if(khsjhm.matches("^1[0-9 ]{10,12}$")){
            b = true;
        }
        return b;
    }
    //检查客户联系人格式
    private Boolean checkKhlxr(String  khlxr){
        Boolean b = false;
        if(khlxr.matches("^[\\u4E00-\\u9FA5A-Za-z]+$")){
            b = true;
        }
        return b;
    }
    //检查联系电话格式
    private Boolean checkKhlxdh(String khlxdh){
        Boolean b = false;
        if(khlxdh.matches("(^(\\d{2,4}[-_－—]?)?\\d{3,8}([-_－—]?\\d{3,8})?([-_－—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)")){
            b = true;
        }
        return b;
    }
    //检查传真号码格式
    private Boolean checkKhczhm(String khczhm){
        Boolean b = false;
        if(khczhm.matches("(^(\\d{3,4}-)?\\d{7,8}$)")){
            b = true;
        }
        return b;
    }
    //检查税号格式
    private Boolean checkShuihao(String  shuihao){
        Boolean b = false;
        if(shuihao.matches("^[A-Z0-9 ]{1,24}$")){
            b = true;
        }
        return b;
    }

    //检查联系地址格式
    private Boolean checkLxdz(String lxdz){
        Boolean b = false;
        if(lxdz.matches("^[\\w\\u4E00-\\u9FA5()&]{1,50}$")){
            b = true;
        }
        return b;
    }
    //检查客户简称格式
    private Boolean checkKhjc(String khjc){
        Boolean b = false;
        if(khjc.matches("^.{1,10}$")){
            b = true;
        }
        return b;
    }
    //检查客户助记码格式
    private Boolean checkKhzjm(String khzjm){
        Boolean b = false;
        if(khzjm.matches("^.{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查客户全称格式
    private Boolean checkKhqc(String khqc){
        Boolean b = false;
        if(khqc.matches("^.{1,30}$")){
            b = true;
        }
        return b;
    }
}
